package net.garbage;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Jose Martin Rozanec;
 */
public abstract class NetServer extends Thread {
    private int portnum;
    private String serverName;

    public NetServer(int portnum) {
        this.portnum = portnum;
        start();
    }

    public void run() {
        ServerSocket server_socket = null;
        try {
            server_socket = new ServerSocket(portnum);
            while (true) {
                createClientPort(server_socket.accept());
            }
        } catch (IOException ioerror) {
            System.err.println("IO Error opening server socket");
        } finally {
            if (server_socket != null) {
                try {
                    server_socket.close();
                }
                catch (IOException ioe) {
                }
            }
        }
    }

    //todo ver uso
    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    //todo END OF ver uso
    protected abstract void createClientPort(Socket socket);

}